package galatea;

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class GraphicWindow extends JPanel implements MouseMotionListener{

	Point lastModLoc;
	
	GraphicWindow(){
		this.setLayout(null);
		this.addMouseMotionListener(this);
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
		if (e.getSource() instanceof GraphicWindow){
			//System.out.println(e.getPoint());
			//ListObject obj = (ListObject)e.getSource();
			//obj.getParent().setLocation(e.getPoint());
			for (Component o:this.getComponents()){
				ListObject obj = (ListObject)o;
				if (obj.dragging){
					//System.out.println(obj.getText() + "is being dragged");
					obj.setLocation(e.getPoint());
				}
			}
			repaint();
		}
		if (e.getSource() instanceof ListObject){
			for (Component o:this.getComponents()){
				ListObject obj = (ListObject)o;
				if (obj.dragging){
					//System.out.println(obj.getText() + " is being dragged");
					if (obj.justPressed){
						lastModLoc = obj.getLocation();
						//System.out.println(lastModLoc);
						obj.justPressed = false;
					}
					else{
						obj.setLocation(lastModLoc.x + e.getX() - obj.clickPoint.x, lastModLoc.y + e.getY() - obj.clickPoint.y);
						obj.justPressed = true;
					}
				}
			}
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void addMod(ListObject newMod, Color color) {
		newMod.setBackground(color);
		newMod.setOpaque(true);		
		newMod.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		//newMod.setText("\n\r" + "  " + newMod.getText() + "  " + "\n\r");
		newMod.setSize(100, 100);
		newMod.setLocation(0 + newMod.getWidth()/2, 0 + newMod.getHeight()/2);
		
		newMod.addMouseListener(newMod);
		this.add(newMod);
	}
}
